<?php
//
//namespace app\controllers;
//
//use app\models\FloorModel;
//use Yii;
//use app\models\Floor;
//use app\models\activeq\FloorSearch;
//use yii\helpers\FileHelper;
//use yii\helpers\Url;
//use yii\web\Controller;
//use yii\web\NotFoundHttpException;
//use yii\filters\VerbFilter;
//
///**
// * FloorController implements the CRUD actions for Floor model.
// */
//class FloorController extends Controller
//{
//    /**
//     * @inheritdoc
//     */
//    public function behaviors()
//    {
//        return [
//            'verbs' => [
//                'class' => VerbFilter::className(),
//                'actions' => [
//                    'delete' => ['POST'],
//                ],
//            ],
//        ];
//    }
//
//    public function actionTest(){
//        file_put_contents("CAD/test.png","test");
//        return Yii::$app->homeUrl;
//    }
//
//    static public function base64_upload($base64, $name) {
//        $base64_image = str_replace(' ', '+', $base64);
//        if(file_put_contents("CAD/".$name.".png", base64_decode($base64_image))){
//            return "CAD/".$name.".png";
//        }
//        else{
//            return false;
//        }
//
//        //post的数据里面，加号会被替换为空格，需要重新替换回来，如果不是post的数据，则注释掉这一行
////        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
////            //匹配成功
////            if($result[2] == 'jpeg'){
////                $image_name = $name.'.jpg';
////                //纯粹是看jpeg不爽才替换的
////            }else{
////                $image_name = $name.'.'.$result[2];
////            }
////            $image_file = $image_name;
////            //服务器文件存储路径
////            if (file_put_contents(Url::home(true)."CAD/".$image_file, base64_decode(str_replace($result[1], '', $base64_image)))){
////                return Url::home(true)."CAD/".$image_file;
////            }else{
////                return "存储失败".Url::home(true);
////            }
////        }else{
////            return "匹配失败！";
////        }
//    }
//    ///
//    /// @Request $request
//    ///
//    static public function uploadFloor($request, $floor_id = 0){
//        $x = $request->post("x_Axis");
//        $z = $request->post("z_Axis");
//        $tail = $request->post("tail");
//        $door = $request->post("door");
//        $CAD = $request->post("CAD", null);
//        $modelUrl = $request->post("modelUrl");
//        $nodeChild = $request->post("nodeChild");
////        $modelId = $request->post("modelId");
////        if ($modelId == 0 || is_null($modelId)){
////            return "Model Id can't be null";
////        }
////        $model_url = FloorModel::findOne($modelId)->model_url;
//        $CADUrl = "";
//        //return $CAD;
//        FileHelper::createDirectory('CAD/');
//        if ($CAD){
//            $CADUrl = FloorController::base64_upload($CAD, md5(uniqid(mt_rand(), true)));
//            //return $savedOrUrl;
//            if ($CADUrl == false){
//                return "存储失败！";
//            }
//        }
//        else{
//            return "没有CAD图片数据";
//        }
//        if ($floor = Floor::findOne($floor_id)){
//            $floor->x = $x;
//            $floor->z = $z;
//            $floor->tail = $tail;
//            $floor->door = $door;
//
//            $floor->model_url = $modelUrl;
//            $floor->CAD_url = $CADUrl;
//            $floor->nodeChild = $nodeChild;
//            $floor->save();
//            return $floor->id;
//        }
//        else{
//            $floor = new Floor();
//            $floor->x = $x;
//            $floor->z = $z;
//            $floor->tail = $tail;
//            $floor->door = $door;
//            $floor->model_url = $modelUrl;
//            $floor->CAD_url = $CADUrl;
//            $floor->nodeChild = $nodeChild;
//
//            $floor->save();
//            return $floor->id;
//        }
//    }
//
//    public function actionUpload(){
//        $request = \Yii::$app->request;
//        self::uploadFloor($request);
//    }
//    public static function getJson(){
//        $floorModels = FloorModel::find()->all();
//        $floorModelArr = [];
//        for ($i = 0; $i < count($floorModels); ++$i){
//            $floorModelArr[$i]['id'] = $floorModels[$i]->id;
//            $floorModelArr[$i]['model_url'] = Url::home(true).$floorModels[$i]->model_url;
//            $floorModelArr[$i]['img_url'] =Url::home(true).$floorModels[$i]->img_url;
//        }
//        return $floorModelArr;
//    }
//    public function actionJson(){
//        $floorModelArr = $this->getJson();
//        return json_encode($floorModelArr);
//
////        $floors = Floor::find()->all();
////        $floorArr = [];
////        for ($i = 0; $i < count($floors); ++$i){
////            $floorArr[$i]['id'] = $floors[$i]->id;
////            $floorArr[$i]['x'] = $floors[$i]->x;
////            $floorArr[$i]['z'] = $floors[$i]->z;
////            $floorArr[$i]['CADUrl'] = $floors[$i]->CAD_url;
////            $floorArr[$i]['modelUrl'] = $floorArr[$i]->model_url;
////            $floorArr[$i]['sceneId'] = $floors[$i]->scene_id;
////        }
////        return json_encode($floorArr);
//    }
//
//    public function actionFloorjson($id){
//        $floor = Floor::findOne($id);
//        if ($floor){
//            $floorArr = [];
//            $floorArr['id'] = $floor->id;
//            $floorArr['x'] = $floor->x;
//            $floorArr['z'] = $floor->z;
//            $floorArr['CADUrl'] = Url::home(true).$floor->CAD_url;
//            $floorArr['modelUrl'] = Url::home(true).$floor->model_url;
//            return json_encode($floorArr);
//        }
//        else {
//            return "NULL";
//        }
//    }
//
//    /**
//     * Lists all Floor models.
//     * @return mixed
//     */
//    public function actionIndex()
//    {
//        $searchModel = new FloorSearch();
//        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
//
//        return $this->render('index', [
//            'searchModel' => $searchModel,
//            'dataProvider' => $dataProvider,
//        ]);
//    }
//
//    /**
//     * Displays a single Floor model.
//     * @param integer $id
//     * @return mixed
//     */
//    public function actionView($id)
//    {
//        return $this->render('view', [
//            'model' => $this->findModel($id),
//        ]);
//    }
//
//    /**
//     * Creates a new Floor model.
//     * If creation is successful, the browser will be redirected to the 'view' page.
//     * @return mixed
//     */
//    public function actionCreate()
//    {
//        $model = new Floor();
//
//        if ($model->load(Yii::$app->request->post()) && $model->save()) {
//            return $this->redirect(['view', 'id' => $model->id]);
//        } else {
//            return $this->render('create', [
//                'model' => $model,
//            ]);
//        }
//    }
//
//    /**
//     * Updates an existing Floor model.
//     * If update is successful, the browser will be redirected to the 'view' page.
//     * @param integer $id
//     * @return mixed
//     */
//    public function actionUpdate($id)
//    {
//        $model = $this->findModel($id);
//
//        if ($model->load(Yii::$app->request->post()) && $model->save()) {
//            return $this->redirect(['view', 'id' => $model->id]);
//        } else {
//            return $this->render('update', [
//                'model' => $model,
//            ]);
//        }
//    }
//
//    /**
//     * Deletes an existing Floor model.
//     * If deletion is successful, the browser will be redirected to the 'index' page.
//     * @param integer $id
//     * @return mixed
//     */
//    public function actionDelete($id)
//    {
//        $this->findModel($id)->delete();
//
//        return $this->redirect(['index']);
//    }
//
//    /**
//     * Finds the Floor model based on its primary key value.
//     * If the model is not found, a 404 HTTP exception will be thrown.
//     * @param integer $id
//     * @return Floor the loaded model
//     * @throws NotFoundHttpException if the model cannot be found
//     */
//    protected function findModel($id)
//    {
//        if (($model = Floor::findOne($id)) !== null) {
//            return $model;
//        } else {
//            throw new NotFoundHttpException('The requested page does not exist.');
//        }
//    }
//}
